<?php

/**
 * alltosun.com 抱抱币类型控制器 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2014-11-17 下午6:41:46 $
 * $Id$
 */
class Action
{
    //声明每页显示的条数
    private $per_page = 10;

    /**
     * 显示抱抱币类型列表
     */
    public function index() {
        $page = Request::getParam('page_no', 1);
        $status = Request::getParam('status', 1);
        $is_advenced = Request::getParam('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());

        //过滤条件
        $filter = array();
        $filter['status'] = $status;

        //先获取所有的父级分类的id
        $parent_type_list = _model('bbcoin_type')->getList(array('status' => $status, 'parent_id' => 0));

        $parent_ids = array();
        $bbcoin_type_list = array();
        if ($parent_type_list) {
            foreach ($parent_type_list as $key => $value) {
                array_push($parent_ids, $value['id']);
                $bbcoin_type_list[$key]['id'] = $value['id'];
                $bbcoin_type_list[$key]['res_name'] = $value['res_name'];
                $bbcoin_type_list[$key]['type_list'] = _model('bbcoin_type')->getList(array(
                    'status' => $status,
                    'parent_id' => $value['id']
                ));
            }
        }

        Response::assign('bbcoin_type_list', $bbcoin_type_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::assign('is_advenced', $is_advenced);
        Response::display('admin/bbcoin_type_list.html');

    }

    /**
     * 显示添加页面
     */
    public function add() {
        //获取所有的父级类型列表
        $filter = array();
        $filter['status'] = 1;
        $filter['parent_id'] = 0;

        $parent_bbcoin_type_list = _model('bbcoin_type')->getList($filter);

        Response::assign('parent_bbcoin_type_list', $parent_bbcoin_type_list);
        Response::display('admin/add_bbcoin_type.html');
    }

    /**
     * 添加
     */
    public function create() {
        $type_info = Request::getParam('bbcoin_type', array());

        //检测抱抱币类型是否存在
        $bbcoin_type_info = _model('bbcoin_type')->read(array('status' => 1, 'res_name' => $type_info['res_name']));
        if ($bbcoin_type_info) {
            return '抱抱币类型已经存在了';
        }
        //创建数据
        $info = array();
        $info['res_name'] = $type_info['res_name'];
        $info['parent_id'] = $type_info['parent_id'];

        _model('bbcoin_type')->create($info);

        Response::redirect('bbcoin/admin/bbcoin_type.html');

    }

    /**
     * 编辑
     */
    public function edit() {
        $id = Request::getParam('id', 1);
        $bbcoin_type_info = _uri('bbcoin_type', $id);

        if (!$bbcoin_type_info) {
            return '请选择类型';
        }

        if (!$bbcoin_type_info['parent_id']) {
            $info['parent_id'] = 0;
            $info['parent_res_name'] = '顶级分类';
        } else {
            $parent_info = _uri('bbcoin_type', $bbcoin_type_info['parent_id']);
            $info['parent_res_name'] = $parent_info['res_name'];
        }

        $info['id'] = $bbcoin_type_info['id'];
        $info['res_name'] = $bbcoin_type_info['res_name'];

        Response::assign('bbcoin_type_info', $info);
        Response::display('admin/add_bbcoin_type.html');
    }

    /**
     * 更新
     */
    public function update() {
        $id = Request::getParam('id', 1);
        $bbcoin_type = Request::getParam('bbcoin_type', array());
        //检测抱抱币类型是否存在
        $bbcoin_type_info = _model('bbcoin_type')->read(array(
            'status' => 1,
            'res_name' => $bbcoin_type['res_name'],
            'parent_id' => $bbcoin_type['parent_id']
        ));
        if (!$bbcoin_type_info) {
            _model('bbcoin_type')->update($id, array('res_name' => $bbcoin_type['res_name']));
        }
        Response::assign('status', 1);
        Response::redirect('bbcoin/admin/bbcoin_type.html');
    }

    /**
     * 伪删除
     */
    public function delete() {
        //接收id
        $id = Request::getParam('id', 0);

        $bbcoin_type_info = _uri('bbcoin_type', $id);
        if (!$bbcoin_type_info['parent_id']) {
            //判断此分类下有没有子分类
            $count = _model('bbcoin_type')->getTotal(array('parent_id' => $bbcoin_type_info['id']));
            if ($count) {
                return '请先删除子类型';
            }

        }

        _model('bbcoin_type')->update($id, array('status' => 0));

        return 'ok';
    }

    /**
     * 还原
     */
    public function change_status() {
        //接收id
        $id = Request::Get('id', 1);

        _model('bbcoin_type')->update($id, array('status' => 1));

        Response::assign('status', 1);
        Response::redirect(AnUrl('bbcoin/admin/bbcoin_type_list.html'));
    }

    /**
     * 检测抱抱币类型是否存在
     */
    public function bbcoin_type_exists() {

        $bbcoin_type = Request::getParam('bbcoin_type', '');

        $filter = array();
        $filter['status'] = 1;
        $filter['res_name'] = $bbcoin_type;

        $bbcoin_type_info = _model('bbcoin_type')->read($filter);

        if ($bbcoin_type_info) {
            echo 'no';
        }
        echo 'ok';
    }

}

?>